#!/bin/bash
############################################################################
# Copyright (C) 2007 Lawrence Livermore National Security, LLC
# Produced at Lawrence Livermore National Laboratory.
# Written by Jim Garlick <garlick@llnl.gov>.
# UCRL-CODE-235119
# 
# This file is part of nfsroot, a network root file system utility.
# 
# nfsroot is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# nfsroot is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with nfsroot; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
#
# rc.nfsroot-init - final initialization on rw root then exec init
#
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
configfile=/etc/sysconfig/nfsroot
INITPROG=/sbin/init
if [ -f $configfile ]; then
   . $configfile
fi
prog=rc.nfsroot-init

mkethzero() {
   echo DEVICE=eth0
   echo ONBOOT=no
   echo BOOTPROTO=dhcp
   echo HWADDR=$(ifconfig eth0 | awk '/HWaddr/ {print $NF}')
}

umask 022

# N.B. /etc/rc.sysinit clears /etc/mtab so no don't bother initializing here

# try to silently unmount /readonly (rc.nfsroot-ram case)
umount /readonly 2>/dev/null

# update /etc/sysconfig/network
echo "${prog}: setting up /etc/sysconfig/network" >&2
if ! [ -e /etc/sysconfig/network ]; then
   echo "${prog}: creating /etc/sysconfig/network" >&2
   touch /etc/sysconfig/network
fi
if ! grep -q NETWORKING /etc/sysconfig/network; then
   echo "NETWORKING=yes" >>/etc/sysconfig/network
fi
if ! grep -q HOSTNAME /etc/sysconfig/network; then
   echo "${prog}: setting HOSTNAME=$(hostname)" >&2
   echo "HOSTNAME=$(hostname)" >>/etc/sysconfig/network
fi
# create ifcfg-eth0 if it doesn't exist
if ! [ -e /etc/sysconfig/network-scripts/ifcfg-eth0 ]; then
   echo "${prog}: creating /etc/sysconfig/network-scripts/ifcfg-eth0" >&2
   mkethzero >/etc/sysconfig/network-scripts/ifcfg-eth0
fi

# pre-init hook for configuration management or whatever
for script in /etc/rc.nfsroot.d/*; do
   if [ -x $script ]; then
      echo "${prog}: executing $script" >&2
      $script >&2
   fi
done

echo "${prog}: passing control to ${INITPROG}" >&2
exec $INITPROG "$@"
return 1
